package com.minsf.rpc.protocol;

import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * @Description 协议消息状态枚举 包括请求、响应、心跳、其他
 * @Author minify
 * @Date 2024/3/29 21:04
 * @Project sf-rpc
 */
@Getter
@AllArgsConstructor
public enum ProtocolMessageTypeEnum {


    REQUEST(0),
    RESPONSE(1),
    HEARTH_BEAT(2),
    OTHERS(3);

    private final int key;

    /**
     * 通过 value 获取枚举
     */
    public static ProtocolMessageTypeEnum getEnumByValue(int value) {

        ProtocolMessageTypeEnum[] values = values();
        for (ProtocolMessageTypeEnum protocolMessageStatusEnum : values) {
            if (ObjectUtil.equal(value, protocolMessageStatusEnum.key)) {
                return protocolMessageStatusEnum;
            }
        }
        return null;
    }

    public static ProtocolMessageTypeEnum getEnumByKey(String key) {

        ProtocolMessageTypeEnum[] values = values();
        for (ProtocolMessageTypeEnum protocolMessageStatusEnum : values) {
            if (ObjectUtil.equal(key, protocolMessageStatusEnum.key)) {
                return protocolMessageStatusEnum;
            }
        }
        return null;
    }

}
